const dotenv = require('dotenv')
// 加载全局变量
require('./libs/global')
// 加载.env
dotenv.config()

const db = require('./libs/db')

var User = db.model('user')
var Log = db.model('log')

var models = [
  User,
  Log
]

async function migrate () {
  for (var i in models) {
    var model = models[i]
    async function run() {
      await model.sync({
        // force: true
      })
    }
    try {
      await run()
      log.success(`创建模型${model.name}成功`)
    } catch(err) {
      log.error(err)
    }
  }
  // process.exit()

  var user = {
    nickName: '管理员',
    name: 'admin',
    pass: '123456',
    code: '123456',
    type: '1000'
  }
  try {
    var result = await User.createUser(user)
    log.success('创建用户：', result)
  } catch (err) {
    log.error('创建用户失败', err)
  }
  
  db.close()
}

migrate()
